package com.yangoxix.lovegoing.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.yangoxix.lovegoing.page.ActivityPage;
import com.yangoxix.lovegoing.page.CommentPage;
import com.yangoxix.lovegoing.page.StatePage;
import com.yangoxix.lovegoing.pojo.Comment;
import com.yangoxix.lovegoing.pojo.Image;
import com.yangoxix.lovegoing.pojo.State;
import com.yangoxix.lovegoing.pojo.User;
import com.yangoxix.lovegoing.service.ImageService;
import com.yangoxix.lovegoing.service.StateService;
import com.yangoxix.lovegoing.service.UserService;
import com.yangoxix.lovegoing.util.timeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * The type State controller.
 *
 * @program: lovegoing
 * @description: 朋友圈的controller
 * @author: chenzou
 * @create: 2021 -09-22 14:34
 */
@Api(tags = "用户信息管理")
@Controller
@RequestMapping("state")
public class StateController {

    @Autowired
    private StateService service;
    @Autowired
    private ImageService imageService;

    /**
     * Upload state form response entity.
     *
     * @param title         the title
     * @param content       the content
     * @param publisherName the publisher name
     * @return the response entity
     */
    @PostMapping("UploadStateForm")
    @ResponseBody
    public ResponseEntity uploadStateForm(String title, String content, String publisherName) {
        if (service.selectSomeStateByTitle(title)) {
            State state = new State();
            state.setContent(content);
            state.setTitle(title);
            state.setPublisher(publisherName);
            if (service.insertState(state)) {
                //ok
                return new ResponseEntity(
                        "success",
                        HttpStatus.OK);
            } else {
//                500:执行失败
                return new ResponseEntity(
                        "执行失败",
                        HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } else {
//                500:执行失败
            return new ResponseEntity(
                    "执行失败",
                    HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @Autowired
    UserService userService;

    /**
     * Upload img.上传图片
     *
     * @param file       the file图片资源
     * @param stateTitle the state title动态的标题
     * @return the response entity
     */
    @PostMapping("UploadImg")
    @ResponseBody
    public ResponseEntity uploadImg(@RequestParam("image") MultipartFile file, String stateTitle) {
        //代码规范：方法名和参数命名为小驼峰
        String url = imageService.saveImag(file);
    /*    Image image = new Image();
        image.setImgurl(url);
        image.setActivityname(stateTitle);
        boolean flag = service.insertImg(image);*/
        User user = userService.findByUserName(stateTitle);
        user.setPhoto(url);

        boolean flag = user.updateById();
        return flag ? new ResponseEntity(
                "success",
                HttpStatus.OK) : new ResponseEntity(
                "执行失败",
                HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * Gets state info.
     *
     * @return the state info
     */
    @GetMapping("GetStateInfo")
    @ResponseBody
    public ResponseEntity getStateInfo() {
        try {
            List<StatePage> allStateInfo = service.getAllStateInfo();
            return new ResponseEntity(allStateInfo, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("fail", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * Gets sate detail.
     *
     * @param title the title
     * @return the sate detail
     */
    @GetMapping("StateDetail")
    @ResponseBody
    public ResponseEntity getSateDetail(String title) {
        try {
            State stateDetail = service.getStateDetail(title);
            StatePage statePage = new StatePage();
            statePage.setState(stateDetail);
            statePage.setImages(service.getDataFromImg(title));
            statePage.setUserimg(service.getportrait(stateDetail.getPublisher()));
            return new ResponseEntity(
                    statePage, HttpStatus.OK
            );
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(
                    "fail", HttpStatus.INTERNAL_SERVER_ERROR
            );
        }
    }

    /**
     * Get state comment response entity.
     *
     * @param title the title
     * @return the response entity
     */
    @GetMapping("GetComment")
    @ResponseBody
    public ResponseEntity getStateComment(String title){
        try {
            ArrayList<CommentPage> allCommmentInState = service.getAllCommmentInState(title);
            return new ResponseEntity(allCommmentInState,HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("fial",HttpStatus.OK);
        }
    }

    /**
     * Send comment response entity.
     *
     * @param username the username
     * @param title    the title
     * @param content  the content
     * @return the response entity
     */
    @ApiOperation("发送评论")
    @GetMapping("sendComment")
    @ResponseBody
    public ResponseEntity sendComment(String username,String title,String content) {
        try {
            Comment comment = new Comment();
            comment.setContent(content);
            comment.setUsername(username);
            comment.setTime(Date.from(timeUtil.getCurrentTime()));
            comment.setStateid(Integer.toString(service.getIdByTitle(title)));
            boolean b = service.insertComment(comment);
            if (b) {
                return new ResponseEntity(
                        "success", HttpStatus.OK
                );
            } else {
                return new ResponseEntity(
                        "fail", HttpStatus.OK
                );
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(
                    "fail", HttpStatus.OK
            );
        }
    }

    /**
     * Upload potrait response entity.
     *
     * @param file       the file
     * @param username the state title
     * @return the response entity
     */
    @ApiOperation("上传头像")
    @PostMapping("uploadPotrait")
    @ResponseBody
    public ResponseEntity uploadPotrait(@RequestParam("image") MultipartFile file, String username) {
        try {
            String imageurl = service.getportrait(username);
            imageService.deleteImage(imageurl);
            service.deletePotrait(username);
            String url = imageService.saveImag(file);
            Image image = new Image();
            image.setImgurl(url);
            image.setActivityname(username+"->username");
            image.insert();
            boolean flag = service.uploadPotrait(username, url);
            if (flag) {
                return new ResponseEntity("success", HttpStatus.OK);
            }else {
               return new ResponseEntity(
                        "插入数据库失败",HttpStatus.INTERNAL_SERVER_ERROR
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  new ResponseEntity(
                    "上传文件失败",HttpStatus.INTERNAL_SERVER_ERROR
            );
        }

    }
}
